<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/orderset.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>知识库管理<small><font color="#228b22">运动库管理</font></small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>知识库管理</el-breadcrumb-item>
            <el-breadcrumb-item><font color="#228b22">运动库管理</font></el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="box ordersetting">
                <el-card class="box-card">
                    <div class="boxMain">
                        <el-button  style="margin-bottom: 20px;margin-right: 20px" @click="downloadTemplate()" type="success">空模板下载</el-button>
                        <el-button  style="margin-bottom: 20px;margin-right: 20px" type="success"@click="exportExcel">导出Excel</el-button>
                        <el-upload action="/exercise/upload.do"
                                   name="excelFile"
                                   :show-file-list="false"
                                   :on-success="handleSuccess"
                                   :before-upload="beforeUpload">
                            <el-button type="success">上传文件</el-button>
                        </el-upload>
                    </div>
                    <div>
                        <font size="3" color="#006400"><b><font color="#a52a2a">操作说明：</font>如需查看/修改当前页数据,进行导出Excel操作; 对当前页没有想法,请下载空模板自由发挥;二者操作完成后均需进行点击"上传文件"按钮上传模板文件。</b></font>
                    </div>
                </el-card>

            </div>
            <div class="filter-container">
                <el-input placeholder="运动名称/运动助记码" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="handleCurrentChange(1)" class="dalfBut" type="primary" icon="el-icon-search">搜索</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="helpCode" label="运动助记码" align="center"></el-table-column>
                <el-table-column prop="name" label="运动名称" align="center"></el-table-column>
                <el-table-column prop="count" label="运动次数" align="center"></el-table-column>
                <el-table-column prop="time" label="运动时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: ''//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },

        methods: {
            //上传之前进行文件格式校验
            beforeUpload(file){
                const isXLS = file.type === 'application/vnd.ms-excel';
                if(isXLS){
                    return true;
                }
                const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                if (isXLSX) {
                    return true;
                }
                this.$message.error('上传文件只能是xls或者xlsx格式!');
                return false;
            },
            //下载模板文件
            downloadTemplate(){
                window.location.href="../template/exercise_template.xls";
            },
            //上传成功提示
            handleSuccess(response, file) {
                if(response.flag){
                    this.$message({
                        message: response.message,
                        type: 'success'
                    });
                }else{
                    this.$message.error(response.message);
                }
                this.findPage();
            },
            //分页查询
            findPage() {
                let param = {
                    pageSize: this.pagination.pageSize,
                    currentPage: this.pagination.currentPage,
                    queryString: this.pagination.queryString
                };

                axios.post("/exercise/findByPage.do", param).then(res => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //导出Excel报表
            exportExcel(){
                window.location.href = '/exercise/exportBusinessReport.do?pageSize='+this.pagination.pageSize+'&currentPage='+this.pagination.currentPage+'&queryString='+encodeURI(encodeURI(this.pagination.queryString));
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/exercise/deleteById.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    }).finally(() => {
                        this.findPage();
                    })
                }).catch(() => {
                    this.$message('已取消删除');
                });
            },
            //错误信息展示
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },

        }
    })
</script>
</html>